package com.example.onlinemusic.controller;

import com.example.onlinemusic.mapper.LoveMusicMapper;
import com.example.onlinemusic.model.Music;
import com.example.onlinemusic.model.User;
import com.example.onlinemusic.tools.Constant;
import com.example.onlinemusic.tools.ResponseBodyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: H_tao_X
 * Date: 2022-12-08
 * Time: 12:46
 */
@RestController
@RequestMapping("/lovemusic")
public class LoveMusicController {

    @Resource
    private LoveMusicMapper loveMusicMapper;

    /**
     * 收藏音乐，需要获取到 userId 和 musicId，musicId 传入的参数，userId 通过 session 获取
     */
    @RequestMapping("/likeMusic")
    public ResponseBodyMessage<Boolean> likeMusic(@RequestParam String id, HttpServletRequest request) {

        // 字符串变为整数
        int musicId = Integer.parseInt(id);
        System.out.println("musicId:" + musicId);
        // 1. 检查是否登录
        HttpSession session = request.getSession(false); // 获取 session
        // 判断 session 是否为空
        if(session == null || session.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("没有登录!");
            return new ResponseBodyMessage<>(-1,"请先登录!",false);
        }
        // 如果不为空 获取 session 信息
        User user = (User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        System.out.println("userId:" + userId);

        Music music = loveMusicMapper.findLoveMusic(userId,musicId);

        if(music != null) {
            // 如果不等于null，说明之前收藏过当前的音乐,不能进行收藏 TODD:加一个取消收藏的音乐
            return new ResponseBodyMessage<>(-1,"之前收藏过这个音乐",false);
        }
        // 如果等于空，收藏这首音乐 （插入）
        boolean effect = loveMusicMapper.insertLoveMusic(userId,musicId);
        if(effect) {
            return new ResponseBodyMessage<>(0,"收藏成功",true);
        }else {
            return new ResponseBodyMessage<>(-1,"收藏失败",false);
        }
    }


    /**
     * 查询收藏的音乐
     */
    @RequestMapping("/findlovermusic")
    public ResponseBodyMessage<List<Music>> findLoveMusic(@RequestParam(required = false) String musicName,
                                                          HttpServletRequest request) {
       // 1. 检查是否登录
        HttpSession session = request.getSession(false);
        if(session == null || session.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("没有登录");
            return new ResponseBodyMessage<>(-1,"没有登录",null);
        }
        // 如果不为空，进行查询
        // 先获取 userId
        User user = (User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        List<Music> musicList = null;
        if(musicName == null) {
            // 如果等于空，查询所有的音乐
             musicList = loveMusicMapper.findLoveMusicByUserId(userId);

        }else {
            // 如果不等于空，查询 指定收藏的音乐
             musicList = loveMusicMapper.findLoveMusicByKeyAndUid(musicName,userId);
        }
        return new ResponseBodyMessage<>(0,"查询到了所有的歌曲信息",musicList);
    }


    /**
     * 取消收藏音乐
     */

    @RequestMapping("/deletelovemusic")
    public ResponseBodyMessage<Boolean> deleteLoveMusic(@RequestParam String id,
                                                        HttpServletRequest request) {

        // 将 id 转为 整数
        int musicId = Integer.parseInt(id);

        // 拿到 useId，先检查是否 登录
        HttpSession session = request.getSession(false);
        if(session == null || session.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("没有登录");
            return new ResponseBodyMessage<>(-1,"没有登录",false);
        }
        // 如果 session 不等于 空 拿到  use 信息
        User user = (User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();

        // 获取到 userId 和 musicId 后调用 mapper 中的方法 去删除 ，首先判断是否存在
        int ret = loveMusicMapper.deleteLoveMusic(userId,musicId);
        if(ret == 1) {
            System.out.println("取消收藏成功！");
            return new ResponseBodyMessage<>(0,"取消收藏成功",true);

        }else {
            System.out.println("取消收藏失败");
            return new ResponseBodyMessage<>(-1,"取消收藏失败",false);
        }
    }




}
